package com.example.rlw.mvpplugintest.utils;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.provider.Settings;
import android.support.v7.app.AlertDialog;

import permissions.dispatcher.PermissionRequest;

/**
 * 描述：申请权限时 的工具类
 * Created by rlw on 2019/3/6.
 */
public class PermissionsUtils {
    /**
     * 被拒绝并且不再提醒,提示用户去设置界面重新打开权限
     */
    public static void askForPermission(Context context, String titleContent) {
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setTitle(titleContent);
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

            }
        });
        builder.setPositiveButton("设置", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
                intent.setData(Uri.parse("package:" + context.getPackageName())); // 根据包名打开对应的设置界面
                context.startActivity(intent);
            }
        });
        builder.create().show();
    }

    public static void showPermissionsMessage(Context context, PermissionRequest request) {
        new AlertDialog.Builder(context)
                .setMessage("使用此功能需要WRITE_EXTERNAL_STORAGE和RECORD_AUDIO权限，下一步将继续请求权限")
                .setPositiveButton("下一步", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        request.proceed();//继续执行请求
                    }
                }).setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                request.cancel();//取消执行请求
            }
        }).show();
    }
}
